package com.XML.Dom4J;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import org.junit.Test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.rmi.server.ExportException;
import java.util.List;

/**
 * 利用Xpath和dom4j来检索xml文件，有四种方式
 * 1.绝对路径
 * 2.相对路径
 * 3.全文搜索
 * 4.属性查找
 */

public class XpathDemo1 {
    /**
     * 绝对路径方式检索
     * @throws Exception
     */
    @Test
    public void parse01() throws Exception {
        //a、创建解析器对象
        SAXReader saxReader = new SAXReader();
        //b、把XML加载成Document文档对象
        Document read = saxReader.read(new FileInputStream("D:\\code\\weixin\\app\\src\\Contacts.xml"));
        //通过绝对路径检索文档
        List<Node> nodes = read.selectNodes("/contactList/contact/name");
        for (Node node : nodes) {
            //将节点转为元素对象
            Element element=(Element) node;
            System.out.println(element.getTextTrim());
        }
    }



    /**
     * 相对路径方式检索
     * @throws Exception
     */
    @Test
    public void parse02() throws Exception {
        //a、创建解析器对象
        SAXReader saxReader = new SAXReader();
        //b、把XML加载成Document文档对象
        Document read = saxReader.read(new FileInputStream("D:\\code\\weixin\\app\\src\\Contacts.xml"));
        //获得根节点元素
        Element root = read.getRootElement();

        //通过相对路径检索文档（相对于root）
        //List<Node> nodes = read.selectNodes("/contactList/contact/name");
        List<Node> nodes = root.selectNodes("./contact/name");//注意斜杠前面有个点
        for (Node node : nodes) {
            //将节点转为元素对象
            Element element=(Element) node;
            System.out.println(element.getTextTrim());
        }
    }


    /**
     * 全文检索
     * @throws Exception
     */
    @Test
    public void parse03() throws Exception {
        //a、创建解析器对象
        SAXReader saxReader = new SAXReader();
        //b、把XML加载成Document文档对象
        Document read = saxReader.read(new FileInputStream("D:\\code\\weixin\\app\\src\\Contacts.xml"));

        //通过全文检索文档
        //List<Node> nodes = read.selectNodes("/contactList/contact/name");
        List<Node> nodes = read.selectNodes("//name");//全文搜索name节点
        for (Node node : nodes) {
            //将节点转为元素对象
            Element element=(Element) node;
            System.out.println(element.getTextTrim());
        }

        List<Node> nodes1 = read.selectNodes("//contact/name");//在contact元素下面找name（一级目录）
        for (Node node : nodes1) {
            //将节点转为元素对象
            Element element=(Element) node;
            System.out.println(element.getTextTrim());
        }

        List<Node> nodes2 = read.selectNodes("//contact//name");//在contact元素下面找name（全部目录）
        for (Node node : nodes2) {
            //将节点转为元素对象
            Element element=(Element) node;
            System.out.println(element.getTextTrim());
        }
    }


    /**
     * 属性查找
     * @throws Exception
     */
    @Test
    public void parse04() throws Exception {
        //a、创建解析器对象
        SAXReader saxReader = new SAXReader();
        //b、把XML加载成Document文档对象
        Document read = saxReader.read(new FileInputStream("D:\\code\\weixin\\app\\src\\Contacts.xml"));
        //获得根节点元素


        //通过相对路径检索文档（相对于root）
        //List<Node> nodes = read.selectNodes("/contactList/contact/name");
        List<Node> nodes = read.selectNodes("//@id");//全文检索属性对象
        for (Node node : nodes) {
            //将节点转为元素对象
            Attribute attribute= (Attribute) node;
            System.out.println(attribute.getValue());
        }

        List<Node> nodes1 = read.selectNodes("//name[@id]");//全文检索属性对象
        for (Node node : nodes1) {
            //将节点转为元素对象
            Attribute attribute= (Attribute) node;
            System.out.println(attribute.getValue());
        }



    }

    @Test
    public void parse05() throws Exception {
        //a、创建解析器对象
        SAXReader saxReader = new SAXReader();
        //b、把XML加载成Document文档对象
        Document read = saxReader.read(new FileInputStream("D:\\code\\weixin\\app\\src\\Contacts.xml"));
        //获得根节点元素

        //通过相对路径检索文档（相对于root）
        //List<Node> nodes = read.selectNodes("/contactList/contact/name");
        List<Node> nodes1 = read.selectNodes("//name[@gender]");//全文检索属性对象
        for (Node node : nodes1) {
            //将节点转为元素对象
            Element element= (Element) node;
            System.out.println(element.getTextTrim());
        }
    }

}
